//
//  OCDemoViewController.m
//  swift-01
//
//  Created by China Dragon on 2023/8/18.
//

#import "OCDemoViewController.h"
#import "NetWorkTools.h"

@interface OCDemoViewController ()
@property(nonatomic, strong)NetWorkTools *networktool;

@end

// 解除循环引用 ， 需要注意打断引用链条即可
@implementation OCDemoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [self loadData];
    
//    [self loadData2:^(NSString *data) {
//        NSLog(@"响应结果 %@", data);
//        //2023-08-18 10:59:43.205904+0800 swift-01[6756:153853] 响应结果 网络数据
//    }];
    
    
    __weak typeof(self) weakself = self;//解除循环引用 ，方法一: 弱引用
    
//    __unsafe_unretained typeof(self)weakself = self;//解除循环引用 ，方法二
    
//    NetWorkTools *networktool = [[NetWorkTools alloc]init];
    self.networktool = [[NetWorkTools alloc]init];
//    [networktool loadData2:^(NSString * _Nonnull response_data) {
//    [self.networktool loadData2:^(NSString * _Nonnull response_data) {
    [self.networktool loadData3:^(NSString * _Nonnull response_data) {//演示循环引用
//        NSLog(@"%@", response_data);
//        NSLog(@"响应结果 %@ %@", response_data, self.view);
        NSLog(@"响应结果 %@ %@", response_data, weakself.view);
    }];
}

- (void)loadData{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"耗时操作");
        dispatch_sync(dispatch_get_main_queue(), ^{
            NSLog(@"准备主线的回调");
        });
    });
}

- (void)loadData2:(void (^)(NSString *data))finished{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"耗时操作");
        dispatch_sync(dispatch_get_main_queue(), ^{
            NSLog(@"准备主线的回调");
            finished(@"网络数据");
        });
    });
}

- (void)dealloc{
    NSLog(@"OCDemoViewController 控制器 结束");
}

@end
